package com.costmary.spper.util;

import com.costmary.spper.model.Weather;
import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: sen
 * Date: 2010-9-18
 * Time: 21:23:40
 * To change this template use File | Settings | File Templates.
 */
public class YahooParser {
    private static Logger log = Logger.getLogger("YahooParser.class");

    public Weather parse(InputStream inputStream) throws Exception {
        Weather weather = new Weather();
        log.info("Creating XML Reader");
        SAXReader xmlReader = createXMLReader();
        Document doc = xmlReader.read(inputStream);
        log.info("Parsing XML Response");
        weather.setCity(doc.valueOf("/rss/channel/y:location/@city"));
        weather.setRegion(doc.valueOf("/rss/channel/y:location/@region"));
        weather.setCountry(doc.valueOf("/rss/channel/y:location/@country"));
        weather.setCondition(doc.valueOf("/rss/channel/item/y:condition/@text"));
        weather.setTemp(doc.valueOf("/rss/channel/item/y:condition/@temp"));
        weather.setChill(doc.valueOf("/rss/channel/y:wind/@chill"));
        weather.setHumidity(doc.valueOf("/rss/channel/y:atmosphere/@humidity"));
        return weather;
    }

    private SAXReader createXMLReader() {
        Map<String, String> uris = new HashMap<String, String>();
        uris.put("y", "http://xml.weather.yahoo.com/ns/rss/1.0");
        DocumentFactory factory = new DocumentFactory();
        factory.setXPathNamespaceURIs(uris);
        SAXReader xmlReader = new SAXReader();
        xmlReader.setDocumentFactory(factory);
        return xmlReader;
    }


}
